package com.bamatea.reportfrom.controller;

import com.alibaba.fastjson.support.odps.udf.CodecCheck;
import com.bamatea.reportfrom.domain.BdInvclWc;
import com.bamatea.reportfrom.domain.BdInvclWcC;
import com.bamatea.reportfrom.domain.IcGeneralAllWc;
import com.bamatea.reportfrom.domain.PoOrderBWc;
import com.bamatea.reportfrom.service.*;
import com.bamatea.reportfrom.service.poOrderBwcService;
import com.bamatea.reportfrom.util.ResponseUtil;
import com.bamatea.reportfrom.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;


@RestController
@RequestMapping("/bama")
public class caicouController {
    @Autowired
    private BdinvclWcSercive sercive;

    @Autowired
    private poOrderBwcService sercives;

    @Autowired
    private IcgeneralAllWcService icgeneralAllWcService;
    @Autowired
    private com.bamatea.reportfrom.service.poOrderBwcService poOrderBwcService;
    @Autowired
    private BdInvclWccService bdInvclWccService;
    @Autowired
    private com.bamatea.reportfrom.service.icRecordsignWcService icRecordsignWcService;
    @Autowired
    private ExecutorService executorService;
    @Autowired
    private ReportService reportService;

    //获取所有分类
    @GetMapping("/getpinlei")
    public Object getpinlei() {
        List<BdInvclWc> allpinlei = sercive.findtea();
        List<String> datas = new ArrayList<>();
        for (BdInvclWc item : allpinlei) {
            datas.add(item.getOrdinarytype());
        }
        return ResponseUtil.okList(datas);
    }


   /* @GetMapping("/getpinleirq")
    public Object getpinleirq() {
        List<PoOrderBWc> allpinleirq = sercives.findAlls();
        List<String> datas = new ArrayList<>();
        for (PoOrderBWc item : allpinleirq) {
            if(item!=null) {
                datas.add(item.getDauditdate());
            }
        }
        return ResponseUtil.okList(datas);
    }
*/

    @PostMapping("/getCaigouAmount")
    public Object getCaigouAmount(@RequestParam("start") String start, @RequestParam("end") String end) {
        List<PoOrderBWc> list = sercives.findByStartAndEnds(start, end);
        Map<String, Object> map = new HashMap<>();
        BigDecimal sumAmounts = new BigDecimal(0.00);
        BigDecimal Amounts = new BigDecimal(0.00);

        for (PoOrderBWc item : list) {

            PoOrderBWc bySku = sercives.findBySku(item.getInvcode());
            //采购单价
            if (bySku != null) {
                //商品单价
                BigDecimal norgtaxprice = bySku.getNorgtaxprice();
                if (norgtaxprice == null) {
                    norgtaxprice = new BigDecimal(0.00);
                }

                BigDecimal nordernum = bySku.getNordernum();
                if (nordernum == null) {
                    nordernum = new BigDecimal(0.00);
                }
                BigDecimal multiplys = norgtaxprice.multiply(nordernum);
                sumAmounts = sumAmounts.add(multiplys);
            } else {
                BigDecimal norgtaxprice = new BigDecimal(0.00);
                BigDecimal nordernum = new BigDecimal(0.00);
                BigDecimal res = norgtaxprice.multiply(nordernum);
                sumAmounts = sumAmounts.add(res);
            }
            //采购的总金额

        }
        /*System.out.println(sumAmounts);
        return ResponseUtil.ok(sumAmounts);*/


        //每一个品类的总数
        List<BdInvclWc> category = sercive.findAllCategory();
        List<String> list2 = new ArrayList();
        for (BdInvclWc item : category) {
            list2.add(item.getOrdinarytype());
        }
        for (int i = 0; i < list.size(); i++) {
            List<BdInvclWc> all = sercive.findByName(list2.get(i));
            List<String> list3 = new ArrayList<>();
            for (BdInvclWc item : all) {
                list3.add(item.getInvclasscode());
            }

            List<PoOrderBWc> bycodes = sercives.findBycode(list3);
            for (PoOrderBWc item : bycodes) {
                //商品编码
                String invcode1 = item.getInvcode();
                PoOrderBWc bySku2 = sercives.findBySku(invcode1);
                if (bySku2 != null) {
                    BigDecimal norgtaxprice = bySku2.getNorgtaxprice();
                    BigDecimal nordernum = bySku2.getNordernum();

                    BigDecimal res = norgtaxprice.multiply(nordernum);
                    Amounts = Amounts.add(res);
                } else {
                    BigDecimal norgtaxprice = new BigDecimal(0.00);
                    BigDecimal nordernum = new BigDecimal(0.00);

                    BigDecimal res = norgtaxprice.multiply(nordernum);
                    Amounts = Amounts.add(res);
                }

                map.put(list2.get(i), Amounts.divide(sumAmounts,2));
            }
        }
        Map<String, Object> result = new HashMap<>();
        map.put("sumAmounts", sumAmounts);
        map.put("map", map);
        return ResponseUtil.ok(result);
    }

    /*@PostMapping("/getCaigouAmount2")
    public Object getCaigouAmount2(@RequestParam("start") String start, @RequestParam("end") String end) {



    }*/

}